package com.sgcc.sx.pmos.e_sx_mall.fragment;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpopup.XPopup;
import com.sgcc.sx.pmos.arouter.mall.MallArouterPath;
import com.sgcc.sx.pmos.common.base.MvpBaseFragment;
import com.sgcc.sx.pmos.common.bean.EventCenter;
import com.sgcc.sx.pmos.common.bean.PowDetileByChartBean;
import com.sgcc.sx.pmos.common.bean.PowMonth;
import com.sgcc.sx.pmos.common.bean.PowMonthCoye;
import com.sgcc.sx.pmos.common.dialog.PowaBnormalPopView;
import com.sgcc.sx.pmos.common.utils.ArguConstant;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.sx.pmos.e_sx_mall.adapter.PowersetAdapter;
import com.sgcc.sx.pmos.e_sx_mall.bean.DataSelect;
import com.sgcc.sx.pmos.e_sx_mall.contract.PowersetContract;
import com.sgcc.sx.pmos.e_sx_mall.presenter.PowersetPresenter;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;


//立即下单页面中电量端选择的模块页面
public class PowerSetFragment extends MvpBaseFragment<PowersetPresenter> implements PowersetContract.View {
    private PowersetAdapter mPowersetAdapter;
    private RecyclerView mRecycleview;
    private TextView next2, set_bychart;
    public List<PowMonth> mData = new ArrayList<>();
    public static int CHARTSELECT = 1;
    public static int CHARTSELECT_SUCCSS = 2;
    int startMonth;
    int endMonth;
    String year;
    LinearLayout root_linner,left_linner;


    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_power_set;
    }

    @Override
    protected void initViews() {
        super.initViews();
        mRecycleview = mRootView.findViewById(R.id.recycleview);
        next2 = mRootView.findViewById(R.id.next2);
        root_linner=mRootView.findViewById(R.id.root_linner);
        left_linner=mRootView.findViewById(R.id.left_linner);
        next2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!mPresenter.checkPowList(mData)) {
                    String tipickContent=mPresenter.getTotalTipick(mPowersetAdapter.getTotal());
                    if(!TextUtils.isEmpty(tipickContent)){
                         new XPopup.Builder(mContext)
                                .isDestroyOnDismiss(false)
                                .isViewMode(true)
                                .autoOpenSoftInput(false)
                                .asCustom(new PowaBnormalPopView(getContext(), new PowaBnormalPopView.OnListener() {
                                    @Override
                                    public void onPositiveClick(PowaBnormalPopView pop, boolean ischeck) {
                                        pop.dismiss();
                                        // 立即下单
                                        EventBus.getDefault().post(new EventCenter<>(ArguConstant.PLACEORDER_GO_CONTRACT));
                                    }
                                }, "友情提示", tipickContent, "", "继续下单", false,false,true))
                                .show();
                    }else{
                        EventBus.getDefault().post(new EventCenter<>(ArguConstant.PLACEORDER_GO_CONTRACT));
                    }

                }
            }
        });

        set_bychart = mRootView.findViewById(R.id.set_bychart);
        set_bychart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Postcard postcard = ARouter.getInstance()
                        .build(MallArouterPath.MALL_POWSETCHART);
                postcard.withInt("startMonth", startMonth);
                postcard.withInt("endMonth", endMonth);
                postcard.withString("year", year);
                startForActivityResult(PowerSetFragment.this, postcard, CHARTSELECT);
//                ARouter.getInstance()
//                        .build(MallArouterPath.MALL_POWSETCHART)
//                        .navigation();
            }
        });

        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mRecycleview.setLayoutManager(layoutManager);
        mPowersetAdapter = new PowersetAdapter();
        mPowersetAdapter.setItemClickListener(new PowersetAdapter.OnItemClickListener() {
            @Override
            public void OnItemsClickListener(int position, String s) {

            }

            @Override
            public void OnTotalClickListener() {
                Postcard postcard = ARouter.getInstance()
                        .build(MallArouterPath.MALL_POWSETCHART);
                postcard.withInt("startMonth", startMonth);
                postcard.withInt("endMonth", endMonth);
                postcard.withString("year", year);
                startForActivityResult(PowerSetFragment.this, postcard, CHARTSELECT);
            }
        });

    }


    @Override
    protected PowersetPresenter createPresenter() {
        return new PowersetPresenter();
    }

    @Override
    public void querySuccess() {

    }

    @Override
    public void queryFailed() {

    }


    private void getDataRequest(int startMonth, int endMonth) {
        this.startMonth = startMonth;
        this.endMonth = endMonth;
        for (int i = startMonth; i < endMonth + 1; i++) {
            PowMonth powMont = new PowMonth();
            powMont.setMonthDate(i);
            mData.add(powMont);
        }
    }

    public void setSelectData(DataSelect dataSelect) {
        LogUtils.v(dataSelect.getStartdata() + "------" + dataSelect.getEnddata());
        int startMonth = Integer.valueOf(dataSelect.getStartdata().substring(5, 7));
        int endMonth = Integer.valueOf(dataSelect.getEnddata().substring(5, 7));
        year = dataSelect.getStartdata().substring(0, 4);
        getDataRequest(startMonth, endMonth);
//        getDataRequest();
        int width=    root_linner.getWidth();
        int widthleft=    left_linner.getWidth();
        mPowersetAdapter.setYear(dataSelect.getStartdata().substring(0, 4));
        if(mData.size()!=0){
            if(mData.size()<3&&mData.size()!=0){
                int tureWidth=(int)(width-widthleft)/mData.size();
                mPowersetAdapter.setItemWidth(tureWidth);

            }
            mPowersetAdapter.setList(mData);
            mPowersetAdapter.notifyDataSetChanged();
            mRecycleview.setAdapter(mPowersetAdapter);
        }else{
            ToastUtils.showShort("时间选择有误");
        }

    }


    public List<PowMonth> getPowMonthList() {
        return mData;
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CHARTSELECT) {
            if (resultCode == CHARTSELECT_SUCCSS) {
                PowDetileByChartBean bean = (PowDetileByChartBean) data.getSerializableExtra("monthList");
                List<PowMonthCoye> month = bean.getList();
                initMonthData(month);
                mPowersetAdapter.setList(mData);
                mPowersetAdapter.notifyDataSetChanged();
            }
        }
    }

    private void initMonthData(List<PowMonthCoye> month) {
        mData.clear();
        for (int i = 0; i < month.size(); i++) {
            mData.add(month.get(i).getPowMonth());
        }
    }
}